import {
  organizationList,
  getWarehouseOptions,
  getCountryOption,
  getCurrency,
  getShop
} from '@/api/public'
import cache from '@/plugins/cache'

const params = {
  pageNum: 1, pageSize: 999
}
const useCommonStore = defineStore(
  'common',
  {
    state: () => ({
      personnelOptions: cache.session.getJSON('personnel') || '', // 人员
      warehouseOption: [], // 仓库列表
      countryOptions: cache.session.getJSON('oms_countryOptions') || '', // 国家
      currencyOptions: '',  // 币种
      shopOptions: '' // 店铺
    }),
    actions: {
      getOrganization() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await organizationList()
            cache.session.setJSON('personnel', res.data)
            this.personnelOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getWarehouseOption() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getWarehouseOptions()
            this.warehouseOption = res.data
            resolve(res.data)
          } catch (e) {
            reject(e)
          }
        })
      },
      getCountryOption() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getCountryOption()
            this.countryOptions = res.data
            cache.session.setJSON('oms_countryOptions', res.data)
            resolve(res.data)
          } catch (e) {
            reject(e)
          }
        })
      },
      getCurrencyList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getCurrency()
            // cache.session.setJSON('currency', res.data)
            this.currencyOptions = res.data
              resolve(res.data)
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getShopList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getShop()
            // cache.session.setJSON('currency', res.data)
            this.shopOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },

    }
  })

export default useCommonStore
